//
//  YXSwitchControl.m
//  WCN
//
//  Created by 杨鑫 on 15/7/8.
//  Copyright (c) 2015年 ShanghaiCize Trade And Business Co., Ltd. All rights reserved.
//

#import "YXSwitchControl.h"
//
#import "Macros.h"

@interface WCOrderSwitchItem : UIButton

@end

@implementation WCOrderSwitchItem

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        self.titleLabel.font = [UIFont systemFontOfSize:13.0];
        
        [self setTitleColor:HexRGB(0x898989) forState:UIControlStateNormal];
        [self setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (void)setSelected:(BOOL)selected
{
//    if (self.isSelected == selected)
//        return;
    
    if (selected) {
        self.backgroundColor = [UIColor clearColor];
    } else {
        self.backgroundColor = [UIColor whiteColor];
    }
    
    [super setSelected:selected];
}

@end

@interface YXSwitchControl ()
{
    NSInteger _selectIndex;

}
@property (strong, nonatomic) NSMutableArray *items;

@end

@implementation YXSwitchControl

- (instancetype)init
{
    self = [super initWithFrame:CGRectMake(0, 0, 140 +70, 30)];
    if (self) {
        _selectedIndex = 0;
        self.layer.cornerRadius = 15;
        self.layer.borderWidth = kOnePX;
        self.layer.borderColor = [UIColor orangeColor].CGColor;
        self.layer.masksToBounds = YES;
        self.layer.backgroundColor = [UIColor orangeColor].CGColor;
        
        self.items = [NSMutableArray array];
        self.titles = @[@"待确认",@"进行中", @"已完成"];
    }
    return self;
}

#pragma mark - Setter

- (void)setTitles:(NSArray *)titles
{
    int index = 0;
    CGRect frame = CGRectMake(0, 0, 70, 30);
    for (NSString *title in titles) {
        WCOrderSwitchItem *item = [[WCOrderSwitchItem alloc] init];
      
        item.frame = frame;
        [item setTitle:title forState:UIControlStateNormal];
        
        [self addSubview:item];
        [self.items addObject:item];
        item.tag = 100 +index;
        item.layer.borderWidth = kOnePX;
        item.layer.borderColor = [UIColor orangeColor].CGColor;
        [item addTarget:self action:@selector(itemTap:) forControlEvents:UIControlEventTouchUpInside];
        if (index == 0) {
            _selectedIndex = 0;
            item.selected = YES;
        }
        index++;
        frame.origin.x += 70;
    }
}

- (void)itemTap:(WCOrderSwitchItem *)sender
{
    WCOrderSwitchItem *preItem = [self viewWithTag:_selectedIndex+100];
    if (preItem == sender) {
        return;
    }
    preItem.selected = NO;
    sender.selected = YES;
    NSInteger index = sender.tag - 100;
    if (self.itemChoosedBlock) {
        self.itemChoosedBlock(index);
    }
    _selectedIndex =sender.tag - 100;
    
}
//
//- (void)setSelectedIndex:(NSInteger)selectedIndex
//{
//   
//    if (selectedIndex == _selectedIndex)
//        return;
//    
//    WCOrderSwitchItem *selected = [self.items objectAtIndex:self.selectedIndex];
//    [selected setSelected:NO];
//    
//    WCOrderSwitchItem *willSelected = [self.items objectAtIndex:selectedIndex];
//    [willSelected setSelected:YES];
//    
//    _selectedIndex = selectedIndex;
//}

@end
